package com.corpcredit.webapp.action;

import java.io.Serializable;
import java.util.Date;

import com.corpcredit.Constants;
import com.corpcredit.model.Jbqk;
import com.corpcredit.model.Pddf;
import com.corpcredit.webapp.action.BasePage;
import com.corpcredit.service.GenericManager;

public class PddfForm extends BasePage implements Serializable {
    private GenericManager<Pddf, Long> pddfManager;
    private Pddf pddf = new Pddf();
    private Long id;
    private Jbqk jbqk;
    public void setJbqk(Jbqk jbqk)
	{
		this.jbqk=jbqk;
	}
    public void setPddfManager(GenericManager<Pddf, Long> pddfManager) {
        this.pddfManager = pddfManager;
    }

    public Pddf getPddf() {
        return pddf;
    }

    public void setPddf(Pddf pddf) {
        this.pddf = pddf;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String delete() {
        pddfManager.remove(pddf.getId());
        addMessage("pddf.deleted");

        return "list";
    }

    public String edit() {
        // Comparison to zero (vs. null) is required with MyFaces 1.2.2, not with previous versions
        if (id != null && id != 0) {
            pddf = pddfManager.get(id);
        } else {
            pddf = new Pddf();
        }

        return "edit";
    }

    public String save() {
        boolean isNew = (pddf.getId() == null || pddf.getId() == 0);
        pddf.setJbqk(jbqk);
        pddf.setUsername(getRequest().getRemoteUser());
        pddf.setOptdate(new Date());
        pddf.setOperation(isNew?Constants.OPERATION_ADD:Constants.OPERATION_UPDATE);
        
        pddfManager.save(pddf);

        String key = (isNew) ? "pddf.added" : "pddf.updated";
        addMessage(key);

        if (isNew) {
            return "list";
        } else {
        	  this.id=this.pddf.getId();
              return edit();
        }
    }
} 